package com.obixlabs.security.credentialsstore.model;

import java.io.Serializable;
import java.security.Principal;
import java.util.Collection;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

@MappedSuperclass
public class BaseUser implements Principal, Serializable
{
	private static final long serialVersionUID = -1767409848963945351L;
	
	@Basic(optional=false)
	@Column(name="user_id",length=100,nullable=false)
	private String userId;
	
	@Basic(optional=false)
	@Column(name="password",length=128,nullable=false)
	private String password;

	@Basic(optional=false)
	@Column(name="email_address",length=50,nullable=false,unique=true)	
	private String emailAddress;
	
	@Transient
	private UserGroups groups;
	
	public BaseUser() {setGroups(new UserGroups());}
	
	public BaseUser(BaseUser copy)
	{
		this();
		setUserId(copy.getUserId());
		setPassword(copy.getPassword());
		setEmailAddress(copy.getEmailAddress());
		
		//copy any new and existing memberships
		Collection<ObixGroup>
			currentGroups = 
				copy.getGroups().getCurrentMemberShips().values();
		for (ObixGroup obixGroup : currentGroups)
			join(obixGroup);
		
		for (ObixGroup obixGroup :copy.getGroups().getNewMemberships())
			join(obixGroup);
	}
		
	@XmlAttribute
	public String getUserId(){return userId;}
	public void setUserId(String userid)
	{this.userId = userid;}

	@XmlAttribute
	public String getPassword(){return password;}
	public void setPassword(String password)
	{this.password = password;}

	@XmlAttribute
	public String getEmailAddress(){return emailAddress;}
	public void setEmailAddress(String emailAddress)
	{this.emailAddress = emailAddress;}

	public String getName(){return getUserId();}

	@XmlElement
	public UserGroups getGroups() {return groups;}
	public void setGroups(UserGroups groups) {this.groups = groups;}
	
	public boolean join(ObixGroup group)
	{return groups.join(group);}
	
	public boolean leave(ObixGroup group)
	{return groups.leave(group);}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((emailAddress == null) ? 0 : emailAddress.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		
		result = prime * groups.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj) return true;
		if (obj == null) return false;
		if (!(obj instanceof BaseUser)) return false;
		
		BaseUser other = (BaseUser) obj;
		if (emailAddress == null) {
			if (other.emailAddress != null) return false;
		} else if (!emailAddress.equals(other.emailAddress)) return false;
		
		if (password == null) {
			if (other.password != null) return false;
		} else if (!password.equals(other.password)) return false;
		
		if (userId == null) {
			if (other.userId != null) return false;
		} else if (!userId.equals(other.userId)) return false;
		
		if (!groups.equals(other.groups)) return false;			
		
		return true;
	}	
	
	@Override
	public String toString(){return getUserId();}
	
}//end class def